package com.xingshentech.ugv.operate.module.app.warehouse;


import android.app.Activity;
import android.content.DialogInterface;

import com.bigkoo.alertview.AlertView;
import com.bigkoo.alertview.OnDismissListener;
import com.bigkoo.alertview.OnItemClickListener;

import com.xingshentech.carry.sync.VersionProgressAbstrancHandler;
import com.xingshentech.repository.udpmsg.bean.response.UpdateVehicle;
import com.xingshentech.repository.udpmsg.handler.UpdateVehicleAbstrancHandler;
import com.xingshentech.ugv.operate.base.BaseActivity;
import com.xingshentech.ugv.operate.module.main.MainActivity;
//import com.xingshentech.ugv.operate.module.utils.LogUtils;
//import com.xingshentech.ugv.operate.widget.WarehouseDialog;
import com.xstech.library.app.AppManager;

import vehicle_communication.VersionProgressOuterClass;


public class UpdateVehicleHandler extends VersionProgressAbstrancHandler {
    private static UpdateVehicleHandler mUpdateVehicleHandler;
    public static boolean showDialog;
//    WarehouseDialog warehouseDialog;

    public static UpdateVehicleHandler getInstance() {
        if (mUpdateVehicleHandler == null) {
            mUpdateVehicleHandler = new UpdateVehicleHandler();
        }
        return mUpdateVehicleHandler;
    }

    @Override
    public void receive(VersionProgressOuterClass.VersionProgressInfo updateVehicle, String msg) {
        AppManager.getAppManager().currentActivity().runOnUiThread(() -> {
//            ((BaseActivity) AppManager.getAppManager().currentActivity()).messageEvent(updateVehicle);
            setDialog();
            if (updateVehicle != null) {
                if (updateVehicle.getStatus() == 3) {
                    hideProgress();
//                    new AlertView("提示", updateVehicle.getRepositoryId() + "同步更新成功\n防止数据还未写到硬盘,请在30秒后重启", "确定", null, null,
//                            AppManager.getAppManager().currentActivity(), AlertView.Style.Alert, (o, position) -> {
//                        if (AppManager.getAppManager().currentActivity() instanceof FileExceptionActivity) {
//                            AppManager.getAppManager().getActivity(WarehouseActivity.class).finish();
//                            AppManager.getAppManager().currentActivity().finish();
//                        }
//                    }).setCancelable(false).show();
                    curAcClassName = "";
                } else {
                    setProgress(updateVehicle);
                }
            } else {
                hideProgress();
                new AlertView("提示", msg + "", "确定", null, null, AppManager.getAppManager().currentActivity(), AlertView.Style.Alert, (o, position) -> {
                }).setCancelable(true).show();

            }

        });
    }

    public String curAcClassName = "";

    public void setDialog() {
        if (!curAcClassName.equals(AppManager.getAppManager().currentActivity().getComponentName().getClassName())) {
//            if (warehouseDialog != null) {
//                warehouseDialog.dismiss();
//                showDialog = false;
//                warehouseDialog = null;
//            }
//            curAcClassName = AppManager.getAppManager().currentActivity().getComponentName().getClassName();
//            warehouseDialog = new WarehouseDialog(AppManager.getAppManager().currentActivity());
//            warehouseDialog.setOnDismissListener(dialog -> {
//                showDialog = false;
//                warehouseDialog = null;
//            });
        }
        showProgress();
    }

    public void setProgress(VersionProgressOuterClass.VersionProgressInfo updateVehicle) {
//        if (warehouseDialog != null) {
//            warehouseDialog.setData(updateVehicle);
//        }
    }

    public void showProgress() {
//        if (warehouseDialog != null && !showDialog) {
//            warehouseDialog.show();
//            showDialog = true;
//        }
    }

    public void hideProgress() {
//        if (warehouseDialog != null) {
//            warehouseDialog.dismiss();
//            showDialog = false;
//            warehouseDialog = null;
//        }
    }


    public void setCancelClickClose() {
//        if (warehouseDialog != null) {
//            warehouseDialog.setCancelClickClose("关闭(手机wifi有变化过,可能获取不到进度)");
//        }
    }

}
